﻿using IcsSEMCtrl.Global.Device;
using IcsSEMCtrl.Service.Logger;
using IcsSEMCtrl.Service.Material;
using IcsSEMCtrl.Util.Client;
using Lifter.Device;
using Lifter.Device.Dispatcher;
using Lifter.Device.Unit;
using Lifter.Global;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Lifter.Gui.Monitor.Popups
{
    public partial class CommandAdd : UIEditForm
    {
        private LifterSEM lftSem = null;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000; // 用双缓冲绘制窗口的所有子控件
                return cp;
            }
        }
        public CommandAdd()
        {
            InitializeComponent();
            if (this.lftSem == null)
            {
                this.lftSem = (LifterSEM)Naming.GetDeviceManager().GetDevice();
            }
            this.LoadcbMaterialIDData();
            this.LoadcbFromData();
            this.LoadcbDestData();
        }

        protected override bool CheckData()
        {
            return CheckEmpty(cbMaterialID, "请输入物料ID")
                   && CheckEmpty(tbMaterialType, "请输入物料类型")
                   && CheckEmpty(tbMaterialLoca, "请输入物料当前位置")
                   && CheckEmpty(cbFrom, "请选择指令起始地")
                   && CheckEmpty(cbDest, "请选择指令目的地");
        }
        public void CreateManualCommand()
        {
            //判断输入的信息是否能创建指令
            string carrierID = this.cbMaterialID.Text;
            string from = this.cbFrom.Text;
            string dest = this.cbDest.Text;
            string outDesc;
            string CommandID = "";
            enumNewTrasferCmdAvailable checkResult = this.lftSem.CheckNewTrasferCmdManual(carrierID,from,dest,out outDesc);
            if (checkResult == enumNewTrasferCmdAvailable.AVAIL)
            {
                TransferWorkItem transferWorkItem = this.lftSem.LDispatcher.GetJobByCarrierID(carrierID);
                if (transferWorkItem == null)
                {
                    CommandID = this.lftSem.LDispatcher.AddTransferQueueManual(carrierID, from, dest, out outDesc);
                }
                else
                {
                    this.ShowWarningDialog($"手动创建物料[{carrierID}]指令时,指令[{transferWorkItem.CommandID}]已存在");
                    LogHelper.loggerInfo($"手动创建物料[{carrierID}]指令时,指令[{transferWorkItem.CommandID}]已存在");
                    return;
                }
            }
            if (string.IsNullOrEmpty(CommandID))
            {
                this.ShowWarningDialog("创建指令失败,错误原因:" + outDesc);
                LogHelper.operationLogger(string.Format("用户[{0}]创建指令失败，失败原因[{1}]", this.lftSem.UserName, outDesc));
                lftSem.InsertUserOperation(lftSem.UserName, string.Format("用户[{0}]创建指令失败，失败原因[{1}]", this.lftSem.UserName, outDesc));
            }
            else
            {
                this.ShowSuccessDialog("创建成功，指令ID为:" + CommandID);
                LogHelper.operationLogger(string.Format("用户[{0}]创建指令成功，指令ID为:{1}",this.lftSem.UserName,CommandID));
                lftSem.InsertUserOperation(lftSem.UserName, string.Format("用户[{0}]创建指令成功，指令ID为:{1}", this.lftSem.UserName, CommandID));
            }
        }
        /// <summary>
        /// 加载  物料ID控件数据
        /// </summary>
        private void LoadcbMaterialIDData()
        {
            this.cbMaterialID.Items.Clear();
            List<string> carriers = Naming.GetCarrierManager().GetCarriersId();
            this.cbMaterialID.Items.AddRange(carriers.ToArray());
        }
        /// <summary>
        /// 加载起始地控件数据
        /// </summary>
        private void LoadcbFromData()
        {
            List<string> fromID = new List<string>();
            fromID = this.lftSem.LDispatcher.GetCommandSources();
            this.cbFrom.Items.AddRange(fromID.ToArray());
        }
        /// <summary>
        /// 加载目的地控件数据
        /// </summary>
        private void LoadcbDestData()
        {
            List<string> destID = new List<string>();
            foreach (LifterPort lifterPort in this.lftSem.GetLifterPortAll().Values)
            {
                if (lifterPort.PortInOutMode == enumLifterPortDirection.OUT)
                {
                    destID.Add(lifterPort.HostID);
                }
            }
            this.cbDest.Items.AddRange(destID.ToArray());
        }

        private void cbMaterialID_TextChanged(object sender, System.EventArgs e)
        {
            string materialID = this.cbMaterialID.Text;
            if (!string.IsNullOrEmpty(materialID))
            {
                Carrier carrier = this.lftSem.GetCarrierByCarrierID(materialID);
                this.tbMaterialType.Text = carrier.CarrierType.Value().ToString();
                this.tbMaterialLoca.Text = carrier.CarrierLoc;
            }
        }
    }
}